﻿using Jermaine.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Jermaine.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// 转换成简单的值类型
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TTarget Parse<TTarget>(this string value) {

            var type = typeof(TTarget);
            if (!type.IsValueType) {
                throw new ArgumentException("TTarget must be a value type");
            }
            if (string.IsNullOrEmpty(value)) {
                return default(TTarget);
            }
            try {
                if (type.IsEnum) {
                    return (TTarget)Enum.Parse(type, value, true);
                } else if (type == typeof(System.Drawing.Color)) {
                    return (TTarget)Convert.ChangeType(GetColor(value), type);
                } else if (type == typeof(System.DateTime)) {
                    return (TTarget)Convert.ChangeType(System.DateTime.Parse(value), type);
                } else {
                    return (TTarget)Convert.ChangeType(value, type);
                }
            } catch {
                return default(TTarget);
            }
        }

        private static System.Drawing.Color GetColor(string value) {
            int val = 0;
            if (Int32.TryParse(value, out val)) {
                return System.Drawing.Color.FromArgb(val);
            } else {
                return System.Drawing.Color.FromName(value);
            }
        }

        /// <summary>
        /// json字符串转成对应的object
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TModel JsonToObject<TModel>(this string value) {
            if (string.IsNullOrEmpty(value)) {
                return default(TModel);
            }
            var o = default(TModel);
            try {
                var serializer = SerializerFactory.GetSerializer<JsonSerializer>();
                return serializer.Parse<TModel>(value);
            } catch {
                return o;
            }
        }

        /// <summary>
        /// xml字符串转成对应的object
        /// </summary>
        /// <param name="value"></param>
        public static TModel XmlToObject<TModel>(this string value) {
            if (string.IsNullOrEmpty(value)) {
                return default(TModel);
            }
            var o = default(TModel);
            try {
                var serializer = SerializerFactory.GetSerializer<XmlSerializer>();
                return serializer.Parse<TModel>(value);
            } catch {
                return o;
            }
        }

        /// <summary>
        /// 正则匹配
        /// </summary>
        /// <param name="value"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static Boolean IsMatch(this string value, string pattern) {
            return new Regex(pattern).IsMatch(value);
        }

        /// <summary>
        /// 正则匹配
        /// </summary>
        /// <param name="value"></param>
        /// <param name="pattern"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Boolean IsMatch(this string value, string pattern, RegexOptions options) {
            return new Regex(pattern, options).IsMatch(value);
        }

        /// <summary>
        /// 正则匹配
        /// </summary>
        /// <param name="value"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static Boolean IsMatch(this string value, Patterns.Pattern pattern) {
            return IsMatch(value, pattern.Regex);
        }
    }


}
